package com.candoit.webapps.pricing.bean.adapters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.persistence.EntityManager;

import com.candoit.webapps.pricing.bean.PricingContext;

public abstract class GenericConverter implements Converter {
	
	private String clase;
	
	private String fieldId;
	
	public Object getAsObject(FacesContext context, UIComponent component,
			String value){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		String query = "select g from " + clase +" g  where g." + fieldId + "= :id";
		try{
			return em.createQuery(query).setParameter("id" , Integer.parseInt(value)).getSingleResult();
		}catch(Exception e){
			return null;
		}
	}

	public abstract String getAsString(FacesContext context, UIComponent component,
			Object value);
	
	public String getClase() {
		return clase;
	}

	public void setClase(String clase) {
		this.clase = clase;
	}

	public String getFieldId() {
		return fieldId;
	}

	public void setFieldId(String fieldId) {
		this.fieldId = fieldId;
	}
	
	
	
	

}
